package com.bw.service;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.UUID;
import com.alibaba.fastjson.JSON;
import com.bw.entity.OrderInfo;
import com.bw.mapper.OrderInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class OrderServiceImpl implements OrderService{
    @Autowired
    OrderInfoMapper orderInfoMapper;


    @Transactional
    @Override
    public OrderInfo addOrderInfo(OrderInfo orderInfo) {
    //用户账号、来源、支付方式非空校验

        Assert.notNull(orderInfo.getUserId(),"账号不为空");
        Assert.notNull(orderInfo.getSourceId(),"来源不为空");
        Assert.notBlank(orderInfo.getPayType(),"支付方式不为空");

        //订单编号自动生成，不能重复
        String orderNum = UUID.randomUUID().toString();
        orderInfo.setOrderNum(orderNum);

        //订单状态，默认为：“未支付”
        Integer status = orderInfo.getStatus();
        if (status == null){
            orderInfo.setStatus(0);
        }

        // 打印方法名称和参数到控制台
        System.out.println("addOrderInfo 的参数是 " + JSON.toJSONString(orderInfo));

        orderInfoMapper.addOrderInfo(orderInfo);

        return orderInfo;
    }

    @Override
    public List<OrderInfo> selectByParam(OrderInfo orderInfo) {


        return orderInfoMapper.selectByParam(orderInfo);
    }
}
